package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.enums.UserLoginStatusEnum;
import cc.rengu.redp.bizimpl.upmp.dao.UpmpUserLoginInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserLoginInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserLoginInfoService;
import cc.rengu.redp.common.utils.AddressUtil;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.HttpContextUtil;
import cc.rengu.redp.common.utils.IPUtil;
import cc.rengu.redp.system.domain.LoginLog;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author System
 */
@Slf4j
@Service("upmpUserLoginInfoService")
@DS("upmp")
public class UpmpUserLoginInfoServiceImpl extends ServiceImpl<UpmpUserLoginInfoMapper, UpmpUserLoginInfo> implements IUpmpUserLoginInfoService {


    @Override
    public void saveLoginInfo(UpmpUserLoginInfo loginInfo) {
        loginInfo.setLoginTime(DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN));
        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
        String ip = IPUtil.getIpAddr(request);
        loginInfo.setLastOprTime(loginInfo.getLoginTime());
        loginInfo.setLoginIp(IPUtil.getIpAddr(request));
        loginInfo.setLoginStatus(UserLoginStatusEnum.LOGIN_IN.getType());
        this.save(loginInfo);

    }

    @Override
    public void updateLogoutTime(UpmpUserLoginInfo loginInfo) {
        this.baseMapper.updateLogoutTime(loginInfo);
    }
}
